package com.example.lanou.mynews.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.ViewGroup;

import com.example.lanou.mynews.db.dao.Titles;
import com.example.lanou.mynews.fragment.news.NormalFrag;
import com.example.lanou.mynews.single.SingleDao;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by dllo on 16/1/11.
 */
public class NewsFragAdapter extends FragmentPagerAdapter {
    private ArrayList<Fragment> data;
    private List<Titles> likeTitle;


    public NewsFragAdapter(FragmentManager fm) {
        super(fm);
        likeTitle = SingleDao.getInstance().getLikeTitle();
        data = new ArrayList<>();
        initData();
    }



    private void initData() {
        for (int i = 0; i <likeTitle.size() ; i++) {
            //TODO url拼接
            data.add(new NormalFrag(likeTitle.get(i).getTid()));
        }
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        super.destroyItem(container, position, object);

    }

    @Override
    public CharSequence getPageTitle(int position) {
        return likeTitle.get(position).getName();
    }

    @Override
    public Fragment getItem(int position) {
        return data.get(position);
    }

    @Override
    public int getCount() {
        return likeTitle.size();
    }

    public void addLike(String title,int pos) {
        SingleDao.getInstance().upDataTitle(title,1);
        likeTitle.add(pos,SingleDao.getInstance().getTitle(title));
        notifyDataSetChanged();

    }

    public void delLike(String title,int pos) {
        SingleDao.getInstance().upDataTitle(title,2);
        likeTitle.remove(pos);
        notifyDataSetChanged();

    }
    public void delLike(String title){
        SingleDao.getInstance().upDataTitle(title,2);
        for (Titles titles : likeTitle) {
            if(titles.getName().equals(title)){
                likeTitle.remove(titles);
                return;
            }
        }
    }

}
